from fontTools.ttLib import TTFont
from PIL import Image, ImageDraw, ImageFont
import io
from ddddocr import DdddOcr

ocr = DdddOcr(show_ad=False)

def get_font_maps(font_path):
    font_maps = {

    }
    font = TTFont(font_path)
    img_font = ImageFont.truetype(font_path, size=25)
    for code, name in font.getBestCmap().items():
        # print(code, chr(code), name)

        img = Image.new("RGB", (32, 32), (255, 255, 255))
        draw = ImageDraw.Draw(img)
        draw.text((3, -3), chr(code), (0, 0, 0), font=img_font)

        img_bytes = io.BytesIO()
        img.save(img_bytes, format="JPEG")

        r = ocr.classification(img_bytes.getvalue())
        font_maps[code] = r

    # print(font_maps)
    return font_maps
if __name__ == '__main__':
    get_font_maps("static/font/96fc7b50b772f52-700.woff2")




font_maps = get_font_maps("static/font/96fc7b50b772f52-700.woff2")




def parse_font(value):
    result = ""
    for i in value:
        if ord(i) in font_maps: # 判断十进制字符是否在字体映射里面
            result += font_maps[ord(i)]# 如果存在，则用映射表中对应的正常字符替换
        else:
            result += i# 如果不存在（不是加密字符），则直接保留原字符
    return result


import requests

res = requests.post('https://www.dongchedi.com/motor/pc/sh/sh_sku_list?aid=1839&app_name=auto_web_pc', data={
"sh_city_name":"全国",
"page":2,
"limit":20
})
for item in res.json()['data']['search_sh_sku_info_list']:
    print(item['car_name'], parse_font(item['car_source_city_name']), parse_font(item['official_price']), parse_font(item['sh_price']))












